package com.minew.beaconset.dobackground;

import com.minew.beaconset.bean.LocaltionLog;
import com.minew.beaconset.db.DbHelper;
import com.minew.beaconset.task.Task;
import com.minew.beaconset.beaconset.comparator.LocalDataCopmarator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取本地数据
 */
public class GetLocalDataTask extends Task {
    @Override
    public Task doTask() {
        List<LocaltionLog> localtionLogs = DbHelper.findAll(LocaltionLog.class);
        Map<String,List<LocaltionLog>> map = new HashMap<>();
        if(localtionLogs!=null) {
            for (LocaltionLog log : localtionLogs) {
                if(map.get(log.getMac())!=null){
                    List<LocaltionLog> logs = map.get(log.getMac());
                    logs.add(log);
                    //按时间顺序
                    Collections.sort(logs,new LocalDataCopmarator());
                    map.put(log.getMac(),logs);
                }else{
                    List<LocaltionLog> logs = new ArrayList<>();
                    logs.add(log);
                    map.put(log.getMac(),logs);
                }
            }
        }
        setResult(map);
        return this;
    }
}
